﻿using System;
using System.Data;
using DataAccessObject;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.FUNC;

namespace ThangNM
{
    public partial class Tool : System.Web.UI.Page
    {
        public ThangNM.PublicInterface.Home_New mtpHome;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                mtpHome = Master as ThangNM.PublicInterface.Home_New;
                mtpHome.setClearMsg();
                if (!IsPostBack)
                    BindData();
            }
            catch (Exception ex)
            {
                if (mtpHome != null)
                    mtpHome.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void BindData()
        {
            Int32 iUID = 0;
            if (Request.QueryString[QueryString.media] != null)
            {
                MediaEO _MediaEO = new MediaEO();
                DataSet dsAll = null;
                if (Int32.TryParse(StringEx.Trim(Request.QueryString[QueryString.media]), out iUID))
                {
                    _MediaEO.PK_iMediaID = iUID;
                    dsAll = (new MediaDAO()).SelectBy_PK_iMediaID(_MediaEO.PK_iMediaID);//, Media_iType_C.Ung_Dung
                }
                if (DataTableEx.DataTable_OK(null, dsAll, 0) == false)
                {
                    //Commons.setRedirect(Page, null);
                    return;
                }
                _MediaEO = (new MediaDAO()).MediaDO(dsAll.Tables[0]);

                if (_MediaEO == null)
                {
                    //Commons.setRedirect(Page, null);
                    return;
                }

                if (!StringEx.Compare(Request.RawUrl.Substring(1), _MediaEO.sUrlRewrite))
                {
                    Commons.setRedirect(Page, _MediaEO.sUrlRewrite);
                    return;
                }
                DataTable dtOther = null;
                if (DataTableEx.DataTable_OK(null, dsAll, 2))
                    dtOther = dsAll.Tables[2];
                Commons.setSEO_ON_PAGE(Page, _MediaEO, this.GetType());
                ArticleUC1.BindData_Tool(_MediaEO);
                ArticleUC1.BindData_Common(null, dtOther, _MediaEO, true);
                Int32 iType = Convert.ToInt16(_MediaEO.iTool);
                ArticleUC1.ToolUC1.PreviewByFunction(iType);
                ArticleUC1.ToolUC1.hfKey.Value = iType.ToString();
            }
            else
            {
                Commons.setRedirect(Page, null);
                return;
            }
        }
    }
}